package handler

import (
	"github.com/gin-gonic/gin"
	"zy5/api/request"
	"zy5/internal/dao"
	"zy5/internal/model"
)

func LikeAdd(c *gin.Context) {
	var req request.LikeAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	var like model.Like
	//dao.GetOneByFields(&model.Like{ShopId: req.ShopId}, &like)
	//if like.Id != 0 {
	//	c.JSON(200, gin.H{
	//		"code": 10000,
	//		"msg":  "当前商品已被收藏",
	//		"data": nil,
	//	})
	//	return
	//}

	result := dao.Create(&model.Like{ShopId: req.ShopId, UserId: req.UserId})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "商品收藏失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 10000,
		"msg":  "商品收藏成功",
		"data": like,
	})

}

func LikeRemove(c *gin.Context) {
	var req request.LikeRemove
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	var like model.Like
	dao.GetOneByFields(&model.Like{Id: req.Id}, &like)
	if like.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前收藏不存在",
			"data": nil,
		})
		return
	}

	result := dao.Delete(&model.Like{Id: like.Id})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}
